import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @Author: XXHH
 * @CreateTime: 2025-04-27
 */
public class Demo38 {
    public static void main(String[] args) {
        BlockingDeque<Integer> queue = new LinkedBlockingDeque<>();
        // 生产者
        Thread producer = new Thread(()->{
            int n = 0;
            while (true){
                try {
                    queue.put(n);
                    System.out.println("生产元素: "+n);
                    n++;
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"producer");
        // 消费者
        Thread customer = new Thread(()->{
            while (true){
                try {
                    int n = queue.take();
                    System.out.println("消费元素:"+n);
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"customer");
        producer.start();
        customer.start();
    }
}
